<html>
<head><meta charset="utf-8"><title>Help with navigating patterns · t-compiler/help · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/index.html">t-compiler/help</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Help.20with.20navigating.20patterns.html">Help with navigating patterns</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="220152270"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Help%20with%20navigating%20patterns/near/220152270" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Nadrieril <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Help.20with.20navigating.20patterns.html#220152270">(Dec 16 2020 at 18:16)</a>:</h4>
<p>I'm working on match checking to fix some diagnostics in the presence of or-patterns. My running example is this:</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">match</span><span class="w"> </span><span class="p">(</span><span class="kc">true</span><span class="p">,</span><span class="w"> </span><span class="nb">None</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="p">(</span><span class="kc">true</span><span class="p">,</span><span class="w"> </span><span class="nb">Some</span><span class="p">(</span><span class="n">_</span><span class="p">))</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="p">{}</span><span class="w"></span>
<span class="w">    </span><span class="p">(</span><span class="kc">false</span><span class="p">,</span><span class="w"> </span><span class="nb">Some</span><span class="p">(</span><span class="kc">true</span><span class="p">))</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="p">{}</span><span class="w"></span>
<span class="w">    </span><span class="p">(</span><span class="kc">true</span><span class="w"> </span><span class="o">|</span><span class="w"> </span><span class="kc">false</span><span class="p">,</span><span class="w"> </span><span class="nb">None</span><span class="w"> </span><span class="o">|</span><span class="w"> </span><span class="nb">Some</span><span class="p">(</span><span class="kc">true</span><span class="w"> </span><span class="c1">// FIXME: should be unreachable</span>
<span class="w">                               </span><span class="o">|</span><span class="w"> </span><span class="kc">false</span><span class="p">))</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="p">{}</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>
<p>When analyzing the last branch, what the algorithm does is try in turn <code>true</code> and <code>false</code> for the left half of the pair, and in each case it gets the info of which subpatterns were unreachable. Here for <code>true</code> the second <code>true|false</code> pattern is unreachable, and for <code>false</code> the second <code>true</code> is unreachable.</p>



<a name="220152333"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Help%20with%20navigating%20patterns/near/220152333" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Nadrieril <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Help.20with.20navigating.20patterns.html#220152333">(Dec 16 2020 at 18:17)</a>:</h4>
<p>I want to find the intersection of those two things in order to report that the second <code>true</code> of the last branch is indeed unreachable</p>



<a name="220152533"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Help%20with%20navigating%20patterns/near/220152533" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Nadrieril <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Help.20with.20navigating.20patterns.html#220152533">(Dec 16 2020 at 18:18)</a>:</h4>
<p>So far I identify those things using <code>Span</code>s, so my first question is if there's any way to find if a span is a parent of another. EDIT: I found <code>Span::contains</code></p>



<a name="220152632"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Help%20with%20navigating%20patterns/near/220152632" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Nadrieril <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Help.20with.20navigating.20patterns.html#220152632">(Dec 16 2020 at 18:19)</a>:</h4>
<p>But then what if the second <code>true | false</code> comes from a macro? Then do I have a way to track inclusion the same way?</p>



<a name="220152792"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Help%20with%20navigating%20patterns/near/220152792" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Nadrieril <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Help.20with.20navigating.20patterns.html#220152792">(Dec 16 2020 at 18:20)</a>:</h4>
<p>I guess that's the really interesting case:</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="n">macro_rules</span><span class="o">!</span><span class="w"> </span><span class="n">t_or_f</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="p">()</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="p">(</span><span class="kc">true</span><span class="w"> </span><span class="o">|</span><span class="w"> </span><span class="kc">false</span><span class="p">)</span><span class="w"> </span><span class="p">};</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
<span class="k">match</span><span class="w"> </span><span class="p">(</span><span class="kc">true</span><span class="p">,</span><span class="w"> </span><span class="nb">None</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="p">(</span><span class="kc">true</span><span class="p">,</span><span class="w"> </span><span class="nb">Some</span><span class="p">(</span><span class="n">_</span><span class="p">))</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="p">{}</span><span class="w"></span>
<span class="w">    </span><span class="p">(</span><span class="kc">false</span><span class="p">,</span><span class="w"> </span><span class="nb">Some</span><span class="p">(</span><span class="kc">true</span><span class="p">))</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="p">{}</span><span class="w"></span>
<span class="w">    </span><span class="p">(</span><span class="kc">true</span><span class="w"> </span><span class="o">|</span><span class="w"> </span><span class="kc">false</span><span class="p">,</span><span class="w"> </span><span class="nb">None</span><span class="w"> </span><span class="o">|</span><span class="w"> </span><span class="nb">Some</span><span class="p">(</span><span class="n">t_or_f</span><span class="o">!</span><span class="p">()))</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="p">{}</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>



<a name="220152909"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Help%20with%20navigating%20patterns/near/220152909" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Nadrieril <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Help.20with.20navigating.20patterns.html#220152909">(Dec 16 2020 at 18:21)</a>:</h4>
<p>I'd like to be able to know that the invocation <code>t_or_f!()</code> contains the <code>true</code> in the macro definition. So spans aren't the right thing probably?</p>



<a name="220175913"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Help%20with%20navigating%20patterns/near/220175913" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Noah Lev <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Help.20with.20navigating.20patterns.html#220175913">(Dec 16 2020 at 21:33)</a>:</h4>
<p>You're using the <code>Span</code>s purely for diagnostics, right?</p>



<a name="220176444"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Help%20with%20navigating%20patterns/near/220176444" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Nadrieril <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Help.20with.20navigating.20patterns.html#220176444">(Dec 16 2020 at 21:38)</a>:</h4>
<p>Yes, this is all purely diagnostics</p>



<a name="220176470"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Help%20with%20navigating%20patterns/near/220176470" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Nadrieril <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Help.20with.20navigating.20patterns.html#220176470">(Dec 16 2020 at 21:38)</a>:</h4>
<p>So if handling macros is too complicated I can skip it</p>



<a name="220178959"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Help%20with%20navigating%20patterns/near/220178959" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Noah Lev <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Help.20with.20navigating.20patterns.html#220178959">(Dec 16 2020 at 21:59)</a>:</h4>
<p>I'm not sure if this is at all helpful to solve your problem, but I wanted to mention that <a href="https://doc.rust-lang.org/nightly/nightly-rustc/rustc_span/struct.Span.html#method.from_expansion"><code>Span</code> has a function to see if it came from a macro expansion</a>.</p>



<a name="220181603"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Help%20with%20navigating%20patterns/near/220181603" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Nadrieril <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Help.20with.20navigating.20patterns.html#220181603">(Dec 16 2020 at 22:20)</a>:</h4>
<p>oh, that sounds helpful! looks like with <code>Span::ctxt</code> I might be able to go up the chain of macro invocations</p>



<a name="220181910"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Help%20with%20navigating%20patterns/near/220181910" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Help.20with.20navigating.20patterns.html#220181910">(Dec 16 2020 at 22:23)</a>:</h4>
<p><span class="user-mention" data-user-id="245339">@Nadrieril</span> is <a href="https://doc.rust-lang.org/nightly/nightly-rustc/rustc_span/struct.Span.html#method.source_callsite">https://doc.rust-lang.org/nightly/nightly-rustc/rustc_span/struct.Span.html#method.source_callsite</a> what you're looking for?</p>



<a name="220184312"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Help%20with%20navigating%20patterns/near/220184312" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Nadrieril <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Help.20with.20navigating.20patterns.html#220184312">(Dec 16 2020 at 22:45)</a>:</h4>
<p>Ohh very potentially yes. Would that return the span of  <code>t_or_f!()</code> above if called on the <code>true</code> defined in the macro but obtained from walking down the pattern?</p>



<a name="220185098"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Help%20with%20navigating%20patterns/near/220185098" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Help.20with.20navigating.20patterns.html#220185098">(Dec 16 2020 at 22:52)</a>:</h4>
<p>yes, I think so</p>



<a name="220186344"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Help%20with%20navigating%20patterns/near/220186344" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Nadrieril <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Help.20with.20navigating.20patterns.html#220186344">(Dec 16 2020 at 23:05)</a>:</h4>
<p>Perfect, thanks a lot!</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>